/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (window.addEventListener) {
    window.addEventListener("load", load, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", load);
}

function load() {

}

function synchron2Mem() {
    wdRequest.request("./synchPrivMod2Mem", null, onSyn);//POST方式,默认返回JSON格式数据
}

function onSyn(rsp) {
    displayTip("#40B3FF","#40B3FF","#E5F5FF","完成同步");
}

function editModule(id) {
    if (id > 0) {// 查看
        wdRequest.request("./getModuleById", "privID=" + id, onGetRsp);//POST方式,默认返回JSON格式数据
    } else {
        $("#addModule").fadeIn();
    }
}

function onGetRsp(rsp) {
    $("#addModule").fadeIn();
    $("#privID").val(rsp.privID);
    $("#privName").val(rsp.privName);
    $("#privCode").val(rsp.privCode);
//    alert(rsp.id);
    $(".option_" + rsp.pid).attr("selected", true);
    displayTip("#40B3FF","#40B3FF","#E5F5FF","在这里添加平台商后台页面或者模块");
}

function onSaveRsp(rsp) {
    if (rsp.error == null) {
        window.location.reload(true);
    } else {
        displayTip("#fc8383", "#fc8383", "#fbeded", "保存失败；" + rsp.error);
    }
}

$(function () {
    $("#save").click(function () {
        var privID = $("#privID").val();
        var privName = $.trim($("#privName").val());
        var privCode = $.trim($("#privCode").val());
        var pid = $("#pid").val();
        var data = "privName=" + privName + "&privCode=" + privCode + "&pid=" + pid;
        if (parseInt(privID) > 0) {
            data = data + "&privID=" + privID;
        }

        wdRequest.request("./saveModule", data, onSaveRsp);//POST方式,默认返回JSON格式数据
    });

    $("#cancel").click(function () {
        initMyform();
        $("#addModule").fadeOut();
    });
    
    $("#sychron").click(function () {
        wdRequest.request("./synchPrivMod2Mem.php", null,onSynchron);//POST方式,默认返回JSON格式数据
    });
    $("#deleteMem").click(function () {
        wdRequest.request("./deleteMem.php", null,onDeleteMem);//POST方式,默认返回JSON格式数据
    });
});


function onDeleteMem(rsp) {
    var message = $("#message");
    message.css({"color": "green", "border-color": "green", "background": "#d9f7e6"});
    message.html("&nbsp;&nbsp;&nbsp;完成操作");
    $("#message_wraper").hide();
    $("#message_wraper").fadeIn();
    document.getElementById("message").scrollIntoView();
}

function onSynchron(rsp) {
    var message = $("#message");
    message.css({"color": "green", "border-color": "green", "background": "#d9f7e6"});
    message.html("&nbsp;&nbsp;&nbsp;同步完成");
    $("#message_wraper").hide();
    $("#message_wraper").fadeIn();
    document.getElementById("message").scrollIntoView();
}

function initMyform() {
    $("#privID").val("");
    $("#privName").val("");
    $("#privCode").val("");
    $(".option_" + 0).attr("selected", true);
}

function deleteModule(id) {
    $(".theme-popover-mask").show();
    var d = dialog({
        title: '提示',
        content: '你确定要删除吗？',
        okValue: '确定',
        ok: function () {
            $(".theme-popover-mask").hide();
            wdRequest.request("./deleteModule", "privID=" + id, onDeleteRsp);
            return false;
        },
        cancelValue: '取消',
        cancel: function () {
            $(".theme-popover-mask").hide();
        }
    });
    d.show();
}

function onDeleteRsp(rsp) {
    if (rsp.result == 1) {
        window.location.reload(true);
    } else {
        displayTip("#fc8383", "#fc8383", "#fbeded", "操作失败；" + rsp.error);
    }
}

/**
 * 显示提示信息
 * @param {type} txtClr 字体颜色
 * @param {type} bdClr 边框颜色
 * @param {type} bgClr 背景颜色
 * @param {type} msg 提示信息
 * @returns {undefined}
 */
function displayTip(txtClr, bdClr, bgClr, msg) {
//    {"color": "#fc8383", "border-color": "#fc8383", "background": "#fbeded"} // 错误提示
    //    {"color": "green", "border-color": "green", "background": "#d9f7e6"} // 成功
//    {"color": "#40B3FF", "border-color": "#40B3FF", "background": "#E5F5FF"} // 正常
    var message = $("#message");
    message.css({"color": txtClr, "border-color": bdClr, "background": bgClr});
    message.html("&nbsp;&nbsp;&nbsp;" + msg);
    $("#message_wraper").hide();
    $("#message_wraper").fadeIn();
//    document.getElementById("message").scrollIntoView();
}